package com.zhz.lib.project.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zhz.lib.project.service.custom.CustomService;
import com.zhz.lib.project.vo.custom.CustomVO;

/**
 * @author LiWei <a href="mailto:liwei2672@gmail.com">liwei2672@gmail.com</a>
 * @version 创建时间：2010-5-2 下午10:54:39
 * 
 */
@Controller
@RequestMapping("/custom.htm")
public class CustomController {
    private static Logger logger = Logger.getLogger(CustomController.class);

    @Autowired
    private CustomService customService;

    @SuppressWarnings("unchecked")
    @RequestMapping(method = RequestMethod.GET)
    public String get(ModelMap model, HttpServletRequest request, HttpServletResponse response) {
        // 获取顶级模块
        List<CustomVO> customVOList = customService.getTopCustom();
        model.put("customVOList", customVOList);

        String idString = request.getParameter("id");
        CustomVO voCustomVO = customService.getCustomByCustomID(idString);
        if (voCustomVO != null) {
            CustomVO topCustomVO = null;

            // 如果是顶级模块
            if (voCustomVO.getLevel().equals("0")) {
                topCustomVO = voCustomVO;
            }
            // 如果是一级模块
            else if (voCustomVO.getLevel().equals("1")) {
                topCustomVO = customService.getCustomByCustomID(voCustomVO.getSortType());
            }

            List<CustomVO> levelOneList = customService
                .getSubCustom(topCustomVO.getCustomId() + "");

            model.put("voCustomVO", voCustomVO);
            model.put("topCustomVO", topCustomVO);
            model.put("levelOneList", levelOneList);

            // 如果是顶级模块
            if (voCustomVO.getLevel().equals("0")) {
                if (levelOneList != null && levelOneList.size() > 0) {
                    model.put("voCustomVO", levelOneList.get(0));
                }
            }

            customService.customHitPlusOne(Integer.parseInt(idString));

            return "custom";
        }
        return null;
    }
}
